package com.my.serviceimpl;

import com.my.bean.OrderVip;
import com.my.bean.Production;
import com.my.bean.ResultPage;
import com.my.bean.VoPage;
import com.my.mapper.OrderVipMapper;
import com.my.service.OrderVipService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.*;

/**
 * <p>
 * 会员档案资料存储（订单） 服务实现类
 * </p>
 *
 * @author 冯政尚
 * @since 2018-08-24
 */
@Service
public class OrderVipServiceImpl extends ServiceImpl<OrderVipMapper, OrderVip> implements OrderVipService {

    //baseMap是mybatis封装的一个mapper直接调用就行你用在注入mapper了


    //实现分页查询
    @Override
    public ResultPage<OrderVip> selectMyPage(Map map,VoPage voPage,long areaid,boolean admin) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<OrderVip> list = baseMapper.selectMyPage(map,paramMap,areaid,admin);
        Integer count = baseMapper.countMyPage(map,paramMap,areaid,admin);
        ResultPage<OrderVip> resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }




    //修改
    public List<OrderVip> selectOrderVipById(Long id){
        return  baseMapper.selectOrderVipById(id);
    }

    //根据状态查询
    @Override
    public String updateOrderState(Long id) {
        if(baseMapper.updateOrderState(id)>0){
            return  "修改成功";
        }
        return  "修改失败";

    }

    //封装成Map  用mybatis plus
    //查询房间的订单
    @Override
    public String selectByHomeId(OrderVip orderVip,long areaid,boolean admin) {
        List<OrderVip> list = new ArrayList<>();
        //把传进去的id和退房的字段,  this在serviceImpl里边值的是mapper,在Mybatis plus插件中;
        //this指在本类中已经注入了mapper,直接用this就行,不用注入Mapper
        //调用Mybatis plus中的selectMap方法把map传进去,是一个集合
        if(admin){
            //先封装成一个Map
            Map map = new HashMap();
            //里边写传参数,一个是房间的id 一个是退房的字段,1是退房2是未退房
            map.put("home_id", orderVip.getHomeId());
            //查出来的是key是数据库的字段,2是未退房
            map.put("whether_home", 2);
           list = this.selectByMap(map);
        }else{
            //先封装成一个Map
            Map map = new HashMap();
            //里边写传参数,一个是房间的id 一个是退房的字段,1是退房2是未退房
            map.put("home_id", orderVip.getHomeId());
            //查出来的是key是数据库的字段,2是未退房
            map.put("whether_home", 2);
            map.put("areaid",areaid);
            list = this.selectByMap(map);

        }
        //如果查出来的订单大于0
        if (list.size() > 0) {
            //就返回该房间已经有订单,
            return "该房间已经有订单";
            //否则 就添加
        } else {
            //添加订单 默认是状态是2未完成
            orderVip.setOrderStatus(2);
            orderVip.setWhetherHome(2);
            orderVip.setAreaid(areaid);
            if (this.insert(orderVip)) {
                return "保存成功";
            } else {
                return "保存失败";
            }
        }
    }


}
